package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.service.CategoryService;
import net.zjitc.utils.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    // 查找分类列表
    @GetMapping("/list")
   public APIResponse<List<Category>> getCategoryList(){
        
        return APIResponse.suc("查询成功",categoryService.getCategoryList());
    }
    // 添加分类
    @PostMapping("/add")
    public APIResponse<Category> addCategory(@RequestBody Category category){
        categoryService.add(category);
        return APIResponse.suc("添加成功",category);
    }
    // 删除分类
    @PostMapping("/del/{id}")
    public APIResponse deleteCategory(@PathVariable Integer id){
        categoryService.delete(id);
        return APIResponse.suc("删除成功",null);
    }
    // 修改分类
    @PostMapping("/save")
    public APIResponse<Category> updateCategory(@RequestBody Category category){
        if(category.getId()==null)
            return APIResponse.err("修改失败","id 不为空");

        categoryService.update(category);

        return APIResponse.suc("修改成功",category);
    }
    @GetMapping("/findCategoryById")
    public APIResponse<Category> findCategoryById(@RequestParam("id") Integer id){
        if(id==null)
            return APIResponse.err("查询失败",null);
        return APIResponse.suc("查询成功",categoryService.findCategoryById(id));
    }


}
